package com.ruoyi.web.controller.gift;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.gift.domain.RoadSign;
import com.ruoyi.gift.domain.dto.RoadSignQuery;
import com.ruoyi.gift.domain.page.PageResult;
import com.ruoyi.gift.domain.vo.RoadSignDetailVO;
import com.ruoyi.gift.service.RoadSignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
/**
 * 路牌信息
 */
@Api("路牌信息管理")
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/road-sign")
public class RoadSignController extends BaseController {

    private final RoadSignService roadSignService;


    /**
     * 分页查询路牌信息
     */
    @GetMapping("/sel")
    public TableDataInfo test(RoadSignQuery query){
        startPage();
        List<RoadSign> list = roadSignService.test(query);
        return getDataTable(list);
    }

    /**
     * 新增路牌信息
     */
    @ApiOperation("新增路牌信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signCode", value = "路牌编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "streetName", value = "所属街道", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "roadName", value = "道路名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "direction", value = "走向", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "location", value = "位置（交叉口位置）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("")
    @PreAuthorize("@ss.hasRole('record')")
    public AjaxResult addRoadSign(@Valid @RequestBody RoadSign roadSign) {
        try {
           return toAjax(roadSignService.addRoadSign(roadSign)) ;
        } catch (Exception e) {
            log.error("新增路牌信息失败：{}", e.getMessage());
            return error("新增路牌信息失败，"+e.getMessage());
        }
    }



    /**
     * 更新路牌信息
     */
    @PreAuthorize("@ss.hasRole('record')")
    @PutMapping("/update")
    public AjaxResult updateRoadSign(@Valid @RequestBody RoadSign roadSign) {
        try {
            return toAjax(roadSignService.updateRoadSign(roadSign));
        } catch (Exception e) {
            log.error("更新路牌信息失败：{}", e.getMessage());
            return error("更新路牌信息失败");
        }
    }
    /**
     * 根据ID查询路牌信息
     */
    @PreAuthorize("@ss.hasRole('record')")
    @GetMapping( "/id" )
    public AjaxResult getRoadSignById(Long roadId) {
        try {
            RoadSign roadSign = roadSignService.getById(roadId);
            if (roadSign == null) {
                return error("路牌信息不存在");
            }
            return success(roadSign);
        } catch (Exception e) {
            log.error("查询路牌信息失败：{}", e.getMessage());
            return error("查询路牌信息失败");
        }
    }



    /**
     * 根据路牌编号查询
     */
/*    @GetMapping("/code/{signCode}")
    public AjaxResult getRoadSignByCode(@PathVariable @NotNull String signCode) {
        try {
            RoadSign roadSign = roadSignService.getBySignCode(signCode);
            if (roadSign == null) {
                return error("路牌信息不存在");
            }
            return success(roadSign);
        } catch (Exception e) {
            log.error("查询路牌信息失败：{}", e.getMessage());
            return error("查询路牌信息失败");
        }
    }*/

    /**
     * 查询路牌列表（分页）
     */
/*    @GetMapping("/list")
    public AjaxResult getRoadSignList(@Valid @RequestBody RoadSignQuery query) {
        try {
            PageResult<RoadSign> result = roadSignService.getRoadSignList(query);
            return success(result);
        } catch (Exception e) {
            log.error("查询路牌列表失败：{}", e.getMessage());
            return error("查询路牌列表失败");
        }
    }*/

    /**
     * 逻辑删除路牌
     */
    @PreAuthorize("@ss.hasRole('record')")
    @DeleteMapping("/{id}")
    public AjaxResult deleteRoadSign(@PathVariable @NotNull Long id) {
        try {
            roadSignService.deleteRoadSign(id);
            return success("路牌删除成功");
        } catch (Exception e) {
            log.error("删除路牌失败: {}", e.getMessage());
            return error("删除路牌失败："+e.getMessage());
        }
    }

    /**
     * 获取路牌详情（包含维修记录）
     */
    /*@GetMapping("/detail/{id}")
    public AjaxResult getRoadSignDetail(@PathVariable @NotNull Long id) {
        try {
            RoadSignDetailVO detail = roadSignService.getRoadSignDetail(id);
            return success(detail);
        } catch (Exception e) {
            log.error("获取路牌详情失败：{}", e.getMessage());
            return error("获取路牌详情失败");
        }
    }*/

    /**
     * 检查路牌编号是否唯一
     */
    /*@GetMapping("/check-code")
    public AjaxResult checkSignCodeUnique(
            @RequestParam @NotNull String signCode,
            @RequestParam(required = false) Long excludeId) {
        try {
            boolean isUnique = roadSignService.isSignCodeUnique(signCode, excludeId);
            return success(isUnique);
        } catch (Exception e) {
            log.error("检查路牌编号唯一性失败：{}", e.getMessage());
            return error("检查路牌编号唯一性失败");
        }
    }*/

    /**
     * 批量查询路牌信息
     */
    /*@PostMapping("/batch")
    public AjaxResult batchAddRoadSign(@Valid @RequestBody List<RoadSign> roadSigns) {
        try {
            for (RoadSign roadSign : roadSigns) {
                roadSignService.addRoadSign(roadSign);
            }
            return success("批量新增路牌信息成功");
        } catch (Exception e) {
            log.error("批量新增路牌信息失败：{}", e.getMessage());
            return error("批量新增路牌信息失败");
        }
    }*/
}